package com.laputa.model.manager;

import java.util.*;
import com.laputa.dao.*;
import com.laputa.dao.model.*;
import com.laputa.system.element.BuildingElement;

public class GroundManager{

	private GroundDao groundDao;
	private UserManager userManager;
	private BuildingManager buildingManager;
	
	public void setGroundDao(GroundDao pd){
		this.groundDao = pd;
	}
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void setBuildingManager(BuildingManager buildingManager) {
		this.buildingManager = buildingManager;
	}
	
	public void delete(String id) {
		groundDao.delete(new Long(id));
	}

	public List getAll() {
		return groundDao.getAll();
	}

	public Ground getGround(String id) {
		return groundDao.getGround(new Long(id));
	}
	
	public com.laputa.model.Ground getGroundObject(String id){
		Ground ground = getGround(id);
		com.laputa.model.Ground groundObject = new com.laputa.model.Ground();
		User owner = userManager.getUser(ground.getOwner().toString());
		User user = userManager.getUser(ground.getUser().toString());
		System.out.println("from groundmanager, get building id = " + ground.getBuilding());
		
		Building buildingObject = buildingManager.getBuilding(ground.getBuilding().toString());
		groundObject.setOwner(owner.getName());
		groundObject.setUser(user.getName());
		groundObject.setBuilding(buildingObject);
		groundObject.setId(new Long(id));
		return groundObject;
	}
	
	//TODO: May use Spring Factory to new the ground object
	public List getGroundObjectsByOwner(String id){
		
		ArrayList groundObjects = new ArrayList();
		List grounds = groundDao.getGroundsByOwner(new Long(id));
		
		for (int i = 0; i < grounds.size(); ++i){
			Ground ground = (Ground)grounds.get(i);
			System.out.println("ground = " + ground.getId() + " " + ground.getOwner());
			
			com.laputa.model.Ground groundObject = new com.laputa.model.Ground();
			User owner = userManager.getUser(ground.getOwner().toString());
			User user = userManager.getUser(ground.getUser().toString());
			Building buildingObject = buildingManager.getBuilding(ground.getBuilding().toString());
			groundObject.setOwner(owner.getName());
			groundObject.setUser(user.getName());
			groundObject.setBuilding(buildingObject);
			groundObject.setId(ground.getId());
			groundObjects.add(groundObject);
		}
		return groundObjects;
	}
	
	//TODO: BuildingElement -> Building the same as User used in register
	public void save(String groundID, BuildingElement be, long groundPrince, int currentTime){
		Long l = new Long(groundID);
		Building building = new Building();
		building.setHolder(l);
		building.setType(be.getType());
		building.setPopulation(be.getPopulation());
		building.setTotallayer(be.getTotalLayer());
		buildingManager.save(building);
		Ground ground = groundDao.getGround(l);
		Long buildingID = (buildingManager.getBuildingByHolder(groundID)).getId();
		ground.setBuilding(buildingID);
		ground.setPrice(groundPrince);
		ground.setUpdatetime(currentTime);
		groundDao.save(ground);
	}
	
	public void saveUserBuildTime(String groundID, String userID, int buildTime) {
		Ground ground = groundDao.getGround(new Long(groundID));
		ground.setUsetime(buildTime);
		ground.setUser(new Long(userID));
		groundDao.save(ground);
	}
	
	public void save(Ground p) {
		groundDao.save(p);
	}
}
